package com.wwwzh.himalayafm.fragment.disfragment.recommend;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.wwwzh.himalayafm.R;
import com.wwwzh.himalayafm.adapter.live_adapter.ProAdapter;
import com.wwwzh.himalayafm.callback.IsScrollButtonListener;
import com.wwwzh.himalayafm.entity.entity_broadtwo.BroadTotalEntity;
import com.wwwzh.himalayafm.retrofit.HttpService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class PageLiveFragment extends Fragment implements IsScrollButtonListener, Callback<BroadTotalEntity> {



    private HttpService service;
    private int pageNum = 1;
    private String provinceCode;
    private RecyclerView recycle;
    private ProAdapter adapter;

    public static PageLiveFragment getInstance(String title){
        Log.d("aaaaa", "getInstance: "+title);
        PageLiveFragment fragment=new PageLiveFragment();
        Bundle bundle=new Bundle();
        bundle.putString("title", title);
        fragment.setArguments(bundle);
        return fragment;
    }

    public PageLiveFragment() {

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_page_live, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        provinceCode = getArguments().getString("title");
        Log.d("vvvv", "onViewCreated: "+provinceCode);
        recycle = (RecyclerView) view.findViewById(R.id.broad_page_recrcler);
        adapter = new ProAdapter(getContext(), this);
        service = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        service.getResult(pageNum, 20, provinceCode, 2).enqueue(this);
        Log.d("vvvv", "onViewCreated2: "+provinceCode);

        recycle.setAdapter(adapter);
    }

    @Override
    public void isScrollbutton() {
            pageNum++;
            Log.d("vvvv", "isScrollbutton: "+provinceCode);
            service.getResult(pageNum, 20, provinceCode, 2).enqueue(this);
    }

    @Override
    public void onResponse(Call<BroadTotalEntity> call, Response<BroadTotalEntity> response) {
        if (response.body().getResult() != null) {
            adapter.addAll(response.body().getResult());
            Log.d("vvvv", "onResponse: "+response.body().getResult().get(0).getProgramName());

        }
    }

    @Override
    public void onFailure(Call<BroadTotalEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();

    }
}
